import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/widgets/common/xindong_app_bar_dialog.dart';
import 'package:xindong/widgets/common/xindong_app_bar_icon_button.dart';
import 'package:xindong/widgets/dialog/xindong_base_sheet.dart';

/// 选择器模型
class XinDongSelectorClass {
  late String key; // 键 - ID
  late String value; // 值 - NAME
  List<XinDongSelectorClass>? nextList; // 数组 - 二级列表

  XinDongSelectorClass({this.key = "", this.value = ""});
}

class XinDongSelectorSheet extends StatefulWidget {
  final String title; // 标题
  final String name; // 名称 - 当前选项
  final List<XinDongSelectorClass> dataList; // 数据源
  final List<XinDongSelectorClass>? selectedList; // 已选数据
  final Function(List<XinDongSelectorClass> selectedList) onDone; // 数据源

  const XinDongSelectorSheet({
    super.key,
    required this.title,
    required this.name,
    required this.dataList,
    required this.selectedList,
    required this.onDone,
  });

  XinDongSelectorSheet.show({
    super.key,
    required this.title,
    required this.name,
    required this.dataList,
    required this.selectedList,
    required this.onDone,
  }) {
    showSmartSheet(sheet: this);
  }

  @override
  State<XinDongSelectorSheet> createState() => _XinDongSelectorSheetState();
}

class _XinDongSelectorSheetState extends State<XinDongSelectorSheet> {
  FixedExtentScrollController provinceScrollController = FixedExtentScrollController(initialItem: 0);
  FixedExtentScrollController cityScrollController = FixedExtentScrollController(initialItem: 0);
  FixedExtentScrollController areaScrollController = FixedExtentScrollController(initialItem: 0);

  late int _currentProvince = 0;
  late int _currentCity = 0;
  late int _currentArea = 0;
  late String resultText = "";

  late List<XinDongSelectorClass> cityList;
  late List<XinDongSelectorClass> areaList;

  void onDone({bool needCallback = false}) {
    XinDongSelectorClass province = widget.dataList[_currentProvince];
    cityList = province.nextList ?? [];
    XinDongSelectorClass city = cityList[_currentCity];
    areaList = city.nextList ?? [];
    XinDongSelectorClass area = areaList[_currentArea];

    resultText = "${province.value} ${city.value} ${area.value}";
    if (needCallback) {
      widget.onDone([province, city, area]);
    }
  }

  void onUpdate({int? provinceIndex, int? cityIndex, int? areaIndex}) {
    if (provinceIndex != null) {
      _currentProvince = provinceIndex;
      XinDongSelectorClass province = widget.dataList[_currentProvince];
      cityList = province.nextList ?? [];
      cityIndex = 0;
    }

    if (cityIndex != null) {
      if (_currentCity == cityIndex) {
        cityScrollController.jumpToItem(cityIndex);
      }
      _currentCity = cityIndex;

      XinDongSelectorClass city = cityList[_currentCity];
      areaList = city.nextList ?? [];
      areaIndex = 0;
    }

    if (areaIndex != null) {
      if (_currentArea == areaIndex) {
        areaScrollController.jumpToItem(areaIndex);
      }
      _currentArea = areaIndex;
    }
    setState(() {});
  }

  @override
  void initState() {
    super.initState();

    onUpdate(provinceIndex: 0, cityIndex: 0, areaIndex: 0);
  }

  @override
  Widget build(BuildContext context) {
    // 3列

    return Container(
      color: DZColors.dialog,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          XinDongDialogAppBar(
            widget.title,
            leading: XinDongAppBarIconButton(onTap: () => SmartDialog.dismiss()),
            onTapDone: () {
              onDone(needCallback: true);
              SmartDialog.dismiss();
            },
          ),

          // /// 当前内容
          // Container(
          //   height: 30,
          //   padding: const EdgeInsets.only(left: 16, right: 16),
          //   alignment: Alignment.topLeft,
          //   child: Row(
          //     children: [
          //       // name
          //       Expanded(child: Text(widget.name, style: const TextStyle(fontSize: 15, color: DZColors.contentColor))),
          //
          //       // result
          //       Text(resultText, style: const TextStyle(fontSize: 14, color: DZColors.mainColor))
          //     ],
          //   ),
          // ),

          // 列表栏
          SizedBox(
            height: 260,
            child: Row(
              children: [
                Expanded(
                  child: getListViewWidget(
                    list: widget.dataList,
                    scrollController: provinceScrollController,
                    onChanged: (int index) {
                      onUpdate(provinceIndex: index);
                    },
                  ),
                ),
                Expanded(
                  child: getListViewWidget(
                    list: cityList,
                    scrollController: cityScrollController,
                    onChanged: (int index) {
                      onUpdate(cityIndex: index);
                    },
                  ),
                ),
                Expanded(
                  child: getListViewWidget(
                    list: areaList,
                    scrollController: areaScrollController,
                    onChanged: (int index) {
                      onUpdate(areaIndex: index);
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 列表
  Widget getListViewWidget({
    required List list,
    required FixedExtentScrollController scrollController,
    required Function(int index) onChanged,
  }) {
    return CupertinoPicker(
      itemExtent: kCellHeight,
      scrollController: scrollController,
      onSelectedItemChanged: onChanged,
      children: list.map((e) => _getItemWidget(e)).toList(),
    );
  }

  /// 列表Item
  Widget _getItemWidget(XinDongSelectorClass model, {bool isSelected = false}) {
    return Container(
      height: kCellHeight,
      // color: isSelected ? selectedColor : normalColor,
      alignment: Alignment.center,
      child: Text(
        model.value,
        style: TextStyle(
            fontSize: 16, fontWeight: FontWeight.normal, color: isSelected ? DZColors.mainColor : DZColors.titleColor),
      ),
    );
  }
}
